﻿using OpcUaHelper;
using OpcUaHelper.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OPCPlatformCore.Managers
{
    public class OPCUaManager
    {
        OpcUaClient opcUaClient;
        public OpcUaClient OpcUaClient 
        {
            get { return opcUaClient; }
        }
        public OPCUaManager()
        {
            opcUaClient = new OpcUaClient();
        }
        //客户端连接需要根据实际需要来连接服务器,以避免额外的性能损失
        public async void Connect(string opcUaUrl)
        {
            try
            {
                //此处异步连接,为保证连接成功,需等待正常返回
               await opcUaClient.ConnectServer(opcUaUrl);
            }
            catch (Exception)
            {
                throw;
            }
        }
        //因为OPCUA是用来远程客户端连接的对象,操作不是非常频繁,所以需要手动关闭连接
        public void CloseConnect()
        {
            try
            {
                opcUaClient.Disconnect();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public object ChangeObjToType(object value, string targetType)
        {
            Type tempType = Type.GetType(targetType);
            return Convert.ChangeType(value, tempType);
        }
        public void ConnectTest()
        {
            using (FormBrowseServer form = new FormBrowseServer())
            {
                form.ShowDialog();
            }
        }
    }
}
